﻿
var LatLng;
var lat, lng;


function showContextMenu(currentLatLng ) {
    var projection;
    var contextmenuDir;
    projection = map.getProjection() ;
    $('.contextmenu').remove();
    contextmenuDir = document.createElement("div");
    contextmenuDir.className  = 'contextmenu';
    var inner = "<a href='javascript:addLocationSelected()'><div class=context>Thêm địa điểm</div></a>";
    inner += "<a href='javascript:searchNearSelected()'><div class=context>Tìm địa điểm gần nhất</div></a>";
    contextmenuDir.innerHTML = inner;
    $(map.getDiv()).append(contextmenuDir);
    LatLng = currentLatLng;
    setMenuXY(currentLatLng);
    contextmenuDir.style.visibility = "visible";
    
    google.maps.event.addListener(map, "click",function(event){
        CloseMenu();
    }); 
}

function getCanvasXY(currentLatLng){
     var scale = Math.pow(2, map.getZoom());
     var nw = new google.maps.LatLng(
         map.getBounds().getNorthEast().lat(),
         map.getBounds().getSouthWest().lng()
     );
     var worldCoordinateNW = map.getProjection().fromLatLngToPoint(nw);
     var worldCoordinate = map.getProjection().fromLatLngToPoint(currentLatLng);
     
     var scrX = Math.floor((worldCoordinate.x - worldCoordinateNW.x) * scale);
     var scrY = Math.floor((worldCoordinate.y - worldCoordinateNW.y) * scale);
     
     return new google.maps.Point(scrX,scrY);
}
  
function setMenuXY(currentLatLng){
    var mapWidth = $('#map_canvas').width();
    var mapHeight = $('#map_canvas').height();
    var menuWidth = $('.contextmenu').width();
    var menuHeight = $('.contextmenu').height();
    var clickedPosition = getCanvasXY(currentLatLng);
    var x = clickedPosition.x ;
    var y = clickedPosition.y ;

    if((mapWidth - x ) < menuWidth)
         x = x - menuWidth;
    if((mapHeight - y ) < menuHeight)
        y = y - menuHeight;

    $('.contextmenu').css('left',x  );
    $('.contextmenu').css('top',y );
};
	
function CloseMenu()	
{
	$('.contextmenu').remove();	
}
	
function addLocationSelected()
{
    addLocationSelectedLL(LatLng);
}
	

function addLocationSelectedLL(LatLng)	
{
    CloseMenu();    
    hideAllPanel();
	showControl('pEdit');
	createMarker(LatLng) ;
	
	showAllEditButton();
	hideControl('btEdit');
	hideControl('btDelete');
	
	setValue('lbLatLng',LatLng);
	setValue('txtNameLocation','');
    setValue('txtAddress','');
    setValue('txtDescription','');
    setValue('listCatalog','0');
    setValue('lbLat','-1');
    setValue('lbLng','-1');
    setValue('lbIdLocation','-1');
	
	var setItem = document.getElementById('txtNameLocation');
	setItem.Focus();
	hideControl('lbMsg');
}

function editMenuSelected(idLocation, idCategory, name, address, comment, lat, lng)
{
    CloseMenu();   
    hideAllPanel();
    showControl('pEdit');
    
    showAllEditButton();
	hideControl('btAdd');	    
    
    setValue('txtNameLocation',name);
    setValue('txtAddress',address);
    setValue('txtDescription',comment);
    setValue('listCatalog',idCategory);
    setValue('lbLat',lat);
    setValue('lbLng',lng);
    setValue('lbIdLocation',idLocation);
    
	hideControl('lbMsg');
}

function doShareLocation()
{
    var user = document.getElementById('ddlUserShare').value;
    var request = new XMLHttpRequest();
    request.open("POST", "Default.aspx", true);
    request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    request.setRequestHeader("action", "share");
    request.send("id="+shareLocationID + "&user="+user);
    
}

function doCancelShareLocation()
{
    hideAllPanel();
    //showControl('pSearch');
    //showControl('pTreeView');
}

function searchNearSelected()
{
    CloseMenu(); 
    var a = $('#lbUserName').val();
    if(a=="" || a=="false")
    {
        alert("Bạn chưa đăng nhập!");
        return;
    }
      
    hideAllPanel();
    showControl('pSearch2');
    
    var panel = document.getElementById("pSearch2Results");
	panel.innerHTML = "";
	//$('#txtSearch2').val() = "";
	//$('#ddlCatelogy2').val() = "-1";
	//$('#ddlNumOfResults').val() = "10";
}